home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / amiexpress / source / doors / sysup / sysup.c < prev   
Encoding:
C/C++ Source or Header  |  1992-12-26  |  10.7 KB  |  522 lines

  1. #include <exec/types.h>
  2. #include <exec/exec.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <functions.h>
  6. #include "work:romconf/doorheader.h"
  7. #define NL NULL
  8. static struct FileLock      *pdir = NL;
  9. static struct FileInfoBlock *dir_info = NL;
  10. void Convert_CTime_To_Date(char *Wstr, char *Wfmtstr, char *DT_temp);
  11. void DoIt();
  12. int EnterDesc(char *FileName,long fsize,int dirnum);
  13. void free_pdir();
  14. void end();
  15. #define gu getuserstring
  16. #define GU gu
  17. #define pm prompt
  18. #define sm sendmessage
  19. #define hk hotkey
  20. char GSTR1[200];
  21. char GSTR3[200];
  22. char GSTR2[200];
  23. long MoveFile(char *Path,char *filename,long filesize,char *Conference_Location);
  24. long FileCopy(char *s,char *d);
  25. long DynAllocate(char **pbuf,long maxbufsize);
  26. long RFreeSpace(char *path);
  27. void MemoryError(void);
  28.  
  29. main(int argc,char *argv[])
  30. {
  31.   char CMD[200];
  32.   char Access[20];
  33.   char FileName[200];
  34.   
  35.   if(argc!=2)
  36.   {
  37.     printf("SysUp v1.2, written by Joseph Hodge\n");
  38.     printf("This is an AmiExpress v1.1w+ module\n");
  39.     exit(0);
  40.   }
  41.   Register(argv[1][0]-'0');
  42.   gu(CMD,DT_NAME);
  43.   gu(Access,DT_SECSTATUS);
  44.  
  45.   sm("",1);
  46.   sm("Local Sysop upload v1.2, written by Joseph Hodge",1);
  47.  
  48.   DoIt();
  49.   Logoff();
  50. }
  51.  
  52. void DoIt()
  53. {
  54.   char Dir[100];
  55. char Path[200];
  56.   FILE *fi,*fo;
  57.   int ndir;
  58.   int First=1;
  59.   char DirName[200];
  60.   int EnterD=0;
  61.   int MoveFiles=0;
  62.   int GotSome=0;
  63.   char BBSZOOM[200];
  64.   char Conference_Location[200];
  65.   gu(Dir,BB_CONFLOCAL);
  66.   strcpy(Conference_Location,Dir);
  67.   gu(BBSZOOM,BB_LOCAL);
  68.   strcat(BBSZOOM,"Zoom/Sysup.data");
  69.   strcat(Dir,"ndirs");
  70.   fi=fopen(Dir,"r");
  71.   if(fi==NULL)
  72.   {
  73.     sm("Files are not supported in this conference.",1);
  74.     return;
  75.   }
  76.   fgets(Dir,80,fi); fclose(fi);
  77.   ndir=atoi(Dir);
  78.   sm("",1);
  79.   hk("Would you like to enter file descriptions for the files [Y/n] >:",Dir);
  80.   if(Dir[0]=='N' || Dir[0]=='n') sm("No.",1);
  81.   else { sm("Yes.",1); EnterD=1; }
  82.   
  83.   if(EnterD)
  84.   {
  85.     sprintf(Dir,"There are %d Dir listings in this conference.",ndir);
  86.     sm(Dir,1);
  87.     sm("Which Dir listing would you like to Update >:",0);
  88.     pm("",Dir,3);
  89.     if(atoi(Dir)>ndir || atoi(Dir)<1)
  90.     {
  91.       sm("",1);
  92.       sm("That Dir listing does not exist!",1);
  93.       return;
  94.     }
  95.     ndir=atoi(Dir);
  96.     GU(Dir,BB_CONFLOCAL);
  97.     sprintf(DirName,"%sdir%d",Dir,ndir);
  98.   }
  99.   sm("",1);
  100.   hk("Would you like the files moved to ULpath [Y/n] >:",Dir);
  101.   if(Dir[0]=='N' || Dir[0]=='n') sm("No.",1);
  102.   else { sm("Yes.",1); MoveFiles=1; }
  103.   if(!MoveFiles && !EnterD)
  104.   {
  105.     sm("Aborting Local Sysop upload no options selected!",1);
  106.     Logoff();
  107.   }
  108.  
  109.   sm("Please specify directory path which contains your local files",1);  
  110.   pm("or [RETURN] to Abort >:",Path,200);
  111.   if(Path[0]=='\0') 
  112.   {
  113.     sm("Aborting!",1); Logoff();
  114.   }
  115.   if(Path[strlen(Path)-1]=='/') Path[strlen(Path)-1]='\0';
  116.   if(access(Path,00))
  117.   {
  118.     sm("That path does not exist!",1);
  119.     Logoff();
  120.     
  121.   }
  122.   if(MoveFiles)
  123.   {
  124.     hk("ARE YOU SURE YOU WISH THE FILES MOVED? [Y/n] >:",Dir);
  125.     if(Dir[0]=='N' || Dir[0]=='n') { sm("No.",1); MoveFiles=0; }
  126.     else sm("Yes.",1);
  127.   }
  128.   if(!MoveFiles && !EnterD)
  129.   {
  130.     sm("Aborting Local Sysop upload no options selected!",1);
  131.     Logoff();
  132.   }
  133.  
  134.   if ((dir_info =(struct FileInfoBlock *)AllocMem((long)sizeof(struct FileInfoBlock),0L)) == NULL)
  135.    {
  136.      sm("Memory Allocation Error.",1);
  137.      Delay(300L);
  138.      Logoff();
  139.    }
  140.   if (! (pdir=(struct FileLock *)Lock(Path,(ULONG)ACCESS_READ)) )
  141.   {
  142.     free_pdir();
  143.      sm("Error, can't lock directory!",1);
  144.      Logoff();
  145.   }
  146.   if ( ! Examine((BPTR)pdir, dir_info) )
  147.   {
  148.     free_pdir();
  149.     sm("Error, can't examine directory!",1);
  150.     Logoff();
  151.   }
  152.   while(ExNext((BPTR)pdir,dir_info))
  153.   {
  154.     if(dir_info->fib_DirEntryType < 0L )
  155.     {
  156.        GotSome=1;
  157. if(First && EnterD)
  158.       {
  159.         sm("",1);sm("Please enter a description, you have eight lines for your description.",1);
  160.         sm("Press return alone to end.",1);
  161.         First=0;
  162.       }
  163.       if(strlen(dir_info->fib_FileName)>12)
  164.       {
  165.         sm("File: ",0);
  166.         sm(dir_info->fib_FileName,0);
  167.         sm(" is too long. Skipping",1);
  168.       }else
  169.       {
  170.         if(EnterD)EnterDesc(dir_info->fib_FileName,dir_info->fib_Size,ndir);
  171.         if(MoveFiles)MoveFile(Path,dir_info->fib_FileName,dir_info->fib_Size,Conference_Location);
  172.  
  173.       }
  174.     }
  175.   }
  176.   if(GotSome==0)
  177.   {
  178.      sm("",1);
  179.      sm("No Files to work.",1);
  180.      sm("",1);
  181.      Logoff();
  182.   }
  183.       
  184.   
  185.  
  186. }
  187.  
  188. int EnterDesc(char *FileName,long fsize,int dirnum)
  189. {
  190.    FILE *fi;
  191.    char str[200];
  192.    char Istr[200];
  193.    char fmtstr[200];
  194.    char temp[80],str2[255];
  195.    char odate[20],fcomment[70],scomment[9][50];
  196.    long Time_system;
  197.    int status;
  198.    int x2;
  199.    Time_system=time(NULL);
  200.    sprintf(fmtstr,"%s",ctime(&Time_system));
  201.    Convert_CTime_To_Date(fmtstr,odate,temp);
  202.    sm("",0);
  203.    while(1)
  204.    {            
  205.                 sm("                                [--------------------------------------------]",1);
  206.                 sprintf(fmtstr,"%-13s %7ld  %s :",FileName,fsize,odate);
  207.                 sm(fmtstr,0);
  208.                     sprintf(fmtstr,"%-13sP%7ld  %s  ",FileName,fsize,odate);
  209.                 pm("",fcomment,44);
  210.                 if(strlen(fcomment)==0) { continue; }
  211.                     strcat(fmtstr,fcomment); strcpy(fcomment,fmtstr);
  212.                 x2=0;
  213.                         do
  214.                     {
  215.                     sm("                                :",0);
  216.                     pm("",scomment[x2],44);
  217.                     x2+=1;
  218.                     } while(strlen(scomment[x2-1])!=0&&x2<7);
  219.                     gu(Istr,BB_CONFLOCAL);
  220.                     sprintf(fmtstr,"%sdir%d",Istr,dirnum);
  221.                     strcat(Istr,"Upload/");
  222.                     strcat(Istr,FileName);
  223.      
  224.       break;
  225.    }
  226.    sm("",1);
  227.    fi=fopen(fmtstr,"a");
  228.    if(fi==NULL) return(1);
  229.    dirnum=0;
  230.    fprintf(fi,"%s\n",fcomment);
  231.    while(dirnum<x2-1)
  232.    {
  233.      fprintf(fi,"                                 %s\n",scomment[dirnum]);
  234.      dirnum++;
  235.    }
  236.    fclose(fi);
  237. }
  238.  
  239. void free_pdir()
  240. {
  241.    if ( pdir )
  242.       {
  243.       UnLock((BPTR)pdir);
  244.       pdir=NL;
  245.        }
  246.    return;   
  247. }
  248.  
  249. Logoff()
  250. {
  251.   sm("",1);
  252.   ShutDown();
  253.   end();
  254. }
  255. void end()
  256. {
  257.   free_pdir();
  258.  if(dir_info!=NL)
  259.   {
  260.    free_pdir();
  261.    FreeMem(dir_info,(long)sizeof(struct FileInfoBlock));
  262.   }
  263.  exit(0);
  264. }
  265.  
  266. void Convert_CTime_To_Date(char *Wstr, char *Wfmtstr, char *DT_temp)
  267. {
  268. int month,day,year;
  269. char s1[10],s2[10],tempdt[30];
  270.  
  271. sscanf(Wstr,"%s %s %d %s %d",&s2,&s1,&day,&tempdt,&year);
  272. strcpy(DT_temp,tempdt);
  273.  
  274. month=0;
  275. if(s1[0]=='D')
  276.     {
  277.     month=12;
  278.     goto NextP;
  279.     }
  280. if(s1[0]=='N')
  281.     {
  282.     month=11;
  283.     goto NextP;
  284.     }
  285. if(s1[0]=='O')
  286.     {
  287.     month=10;
  288.     goto NextP;
  289.     }
  290. if(s1[0]=='S')
  291.     {    
  292.     month=9;
  293.     goto NextP;
  294.     }
  295. if(s1[0]=='A')
  296.     {
  297.     if(s1[1]=='u')
  298.         {
  299.         month=8;
  300.         goto NextP;
  301.         }
  302.     else
  303.         {
  304.         month=4;
  305.         goto NextP;
  306.         }
  307.     }
  308. if(s1[0]=='J')
  309.     {
  310.     if(s1[1]=='a')
  311.         {
  312.         month=1;
  313.         goto NextP;
  314.         }
  315.     else if(s1[2]=='n')
  316.         {
  317.         month=6;
  318.         goto NextP;
  319.         }
  320.     else if(s1[2]=='l')
  321.         {
  322.         month=7;
  323.         goto NextP;
  324.         }
  325.     }
  326. if(s1[0]=='M')
  327.     {
  328.     if(s1[2]=='y')
  329.         {
  330.         month=5;
  331.         goto NextP;
  332.         }
  333.     if(s1[2]=='r')
  334.         {
  335.         month=3;
  336.         goto NextP;
  337.         }
  338.     }
  339. if(s1[0]=='F')
  340.     {
  341.     month=2;
  342.     }
  343.  
  344. NextP:
  345.  
  346. year-=1900;
  347. sprintf(Wfmtstr,"%2d-%2d-%2d",month,day,year);
  348. if(Wfmtstr[0]==' ') { Wfmtstr[0]='0'; }
  349. if(Wfmtstr[3]==' ') { Wfmtstr[3]='0'; }
  350. if(Wfmtstr[6]==' ') { Wfmtstr[6]='0'; }
  351. }
  352. long DynAllocate(char **pbuf,long maxbufsize)
  353. {
  354. do
  355.     {
  356.     *pbuf=(char *)AllocMem(maxbufsize,MEMF_FAST|MEMF_CLEAR);
  357.     maxbufsize=maxbufsize>>1;
  358.     } while(*pbuf==NULL&&maxbufsize>8192);
  359. maxbufsize=maxbufsize<<1;
  360. if(*pbuf!=NULL)
  361.     {
  362.     return(maxbufsize);
  363.     }
  364. *pbuf=(char *)AllocMem(maxbufsize,MEMF_CHIP|MEMF_CLEAR);
  365. if(*pbuf!=NULL)
  366.     {
  367.     return(maxbufsize);
  368.     }
  369. return(0L);
  370. }
  371.  
  372. long FileCopy(char *s,char *d)
  373. {
  374. UBYTE *buf;
  375. long bufsize,stat1,stat2;
  376. BPTR fhs,fhd;
  377.  
  378. if(bufsize=DynAllocate(&buf,1048576))
  379.     {
  380.     if(fhs=Open(d,MODE_OLDFILE))
  381.         {
  382.         Close(fhs);
  383.         FreeMem(buf,bufsize);return(0);
  384.         }
  385.     if(fhs=Open(s,MODE_OLDFILE))
  386.         {
  387.         if(fhd=Open(d,MODE_NEWFILE))
  388.             {
  389.             do
  390.                 {
  391.                 stat1=Read(fhs,buf,bufsize);
  392.                 if(stat1>0)
  393.                     {
  394.                     stat2=Write(fhd,buf,stat1);
  395.                     }
  396.                 } while(stat1>0&&stat2>0);
  397.             if(stat1<0)
  398.                 {
  399.                     sm("",1);
  400.                 sprintf(GSTR3,"ERROR while reading from %s!",s);
  401.                     sm(GSTR3,1);sm("",1);
  402.                 }
  403.             if(stat2<0)
  404.                 {
  405.                     sm("",1);
  406.                 sprintf(GSTR3,"ERROR while writing to %s!",s);
  407.                 sm(GSTR3,1);sm("",1);
  408.                 }
  409.             Close(fhd);
  410.             }
  411.         Close(fhs);
  412.         }
  413.     FreeMem(buf,bufsize);
  414.     }
  415. if(stat1>=0&&stat2>=0)
  416.     {
  417.     return(1);
  418.     }
  419. return(0);
  420. }
  421.  
  422. long MoveFile(char *Path,char *filename,long filesize,char *Conference_Location)
  423. {
  424. long stat,err;
  425. FILE *f;
  426. char *p;
  427. char FN[200];
  428. filesize=filesize+16384;
  429. strcpy(FN,filename);
  430. sprintf(GSTR3,"%sulpaths",Conference_Location);
  431. if(f=fopen(GSTR3,"r"))
  432.     {
  433.     while(p=fgets(GSTR3,80,f))
  434.         {
  435.         GSTR3[81]='\0';
  436.         GSTR3[strlen(GSTR3)-1]='\0'; // removes the \n at end of line
  437.         stat=RFreeSpace(GSTR3);
  438.         if(stat>filesize)
  439.             {
  440.                if(*(Path+(strlen(Path)-1))!=':')
  441.             sprintf(GSTR1,"%s/%s",Path,filename);
  442.                else
  443.                sprintf(GSTR1,"%s%s",Path,filename);
  444.             sprintf(GSTR2,"%s%s",GSTR3,filename);
  445.             if(stat=FileCopy(GSTR1,GSTR2))
  446.                 {
  447.                 fclose(f);
  448.                 DeleteFile(GSTR1);
  449.                 sm("",0);sm(filename,0); sm(" Posted in Directory",1);
  450.                     sm("",1);
  451.                 return(1);
  452.                 }
  453.             }
  454.         } while(p!=NULL);
  455.     fclose(f);
  456.     sm("WARNING!",1);sm("No free space on any path!  Moving to upload dir...",1);
  457.     }
  458. if(*(Path+(strlen(Path)-1))!=':')
  459. sprintf(GSTR1,"%s/%s",Path,filename);
  460. else
  461. sprintf(GSTR1,"%s%s",Path,filename);
  462. sprintf(GSTR2,"%sUPLOAD/%s",Conference_Location,filename);
  463.         if((stat=FileCopy(GSTR1,GSTR2))) DeleteFile(GSTR1);
  464. if(stat==NULL)
  465.     {
  466.     sm("FAILURE!!!  unable to move file ",0);
  467.      sm(filename,0); sm("!",1);
  468.      sm("",1);
  469.     }
  470. if(stat!=NULL)
  471.     {
  472.     sm("",0); sm(filename,0);sm(" Posted in Directory",1);
  473.      sm("",1);
  474.     return(1);
  475.     }
  476. return(0);
  477. }
  478. long RFreeSpace(char *path)
  479. {
  480. BPTR FLock;
  481. struct InfoData *i_data;
  482.  
  483. long stat=0;
  484.  
  485. if(i_data=(struct InfoData *)AllocMem((long)sizeof(struct InfoData),MEMF_CHIP))
  486.     {
  487.     if(FLock = Lock(path,ACCESS_READ))
  488.         {
  489.         if(stat=Info(FLock,i_data))
  490.             {
  491.             stat=(i_data->id_NumBlocks*i_data->id_BytesPerBlock)-(i_data->id_NumBlocksUsed*i_data->id_BytesPerBlock);
  492.             }
  493.         else
  494.             {
  495.                sm("",1);
  496.             sprintf(GSTR3,"Can not get info from %s for free space",path);
  497.             sm(GSTR3,1);
  498.             }
  499.         UnLock(FLock);
  500.         }
  501.     else
  502.         {
  503.           sm("",1);
  504.         sprintf(GSTR3,"Can not find free space for %s",path);
  505.         sm(GSTR3,1);
  506.         }
  507.     FreeMem(i_data,sizeof(struct InfoData));
  508.     }
  509. else
  510.     {
  511.     (void)MemoryError();
  512.     }
  513. return(stat);
  514. }
  515.  
  516. void MemoryError(void)
  517. {
  518. sm("",1);
  519. sm("Could not allocate enough memory for workspace!!!",1);
  520. sm("",1);
  521. }
  522.